home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jtextmouse.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  6.2 KB  |  203 lines

  1. # jtextmouse.tcl - support for Text mouse bindings
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non-profit, noncommercial use.
  5.  
  6. # TO DO:
  7. # ^L
  8. # sentence-manipulation stuff
  9. # case change commands, transposition commands
  10. # commands to do with mark?
  11. # word deletion - fix to use buffer
  12. # generalise movement to copying-to-cutbuffer and deletion
  13. # IMPROVE ENTRY BINDINGS
  14. # literal-insert for entry
  15.  
  16.  
  17. ######################################################################
  18. # global variables:
  19. #
  20. global J_PREFS env
  21. if {! [info exists J_PREFS(bindings)]} {set J_PREFS(bindings) basic}
  22. if {! [info exists J_PREFS(typeover)]} {set J_PREFS(typeover) 1}
  23. #
  24. ######################################################################
  25.  
  26. ######################################################################
  27. # routines to let scanning and pasting to double-up on the same button
  28. # based on code by Tom Phelps <phelps@cs.berkeley.edu>
  29. # modified by jay to make it a little easier to paste
  30. ######################################################################
  31.  
  32. # j:tmb:start_scan_or_paste W x y t - start a drag or paste, recording
  33. #   current location and time so we can later decide whether to paste or drag
  34. # BIND TO <ButtonPress-N>
  35. proc j:tmb:start_scan_or_paste { W x y t } {
  36.   global j_teb
  37.   $W scan mark $y
  38.   set j_teb(scanpaste,time) $t
  39.   set j_teb(scanpaste,x) $x
  40.   set j_teb(scanpaste,y) $y
  41.   set j_teb(scanpaste,pasting) 1
  42. }
  43.  
  44. # j:tmb:continue_scan W x y t - scan the entry, and mark the fact that
  45. #   we're scanning, not pasting.  won't scan if the mouse has moved only
  46. #   one pixel.
  47. # BIND TO <BN-Motion>
  48. proc j:tmb:continue_scan { W x y t } {
  49.   global j_teb
  50.   
  51.   set xdiff [expr {abs($x - $j_teb(scanpaste,x))}]
  52.   set ydiff [expr {abs($y - $j_teb(scanpaste,y))}]
  53.   
  54.   if {$xdiff >= 2 || $ydiff >= 2} {
  55.     $W scan dragto $y
  56.     set j_teb(scanpaste,pasting) 0
  57.   }
  58. }
  59.  
  60. # j:tmb:end_scan_or_paste W x y t - if we haven't been scanning, and it's
  61. #   been less than 500ms since button-down, paste selection
  62. # BIND TO <ButtonRelease-N>
  63. proc j:tmb:end_scan_or_paste { W x y t } {
  64.   global j_teb
  65.   if {$j_teb(scanpaste,pasting) &&
  66.       [expr {$t-$j_teb(scanpaste,time)}] < 500} {
  67.     # j:tb:paste_selection $W
  68.     catch {
  69.       focus $W
  70.       j:text:insert_string $W [selection get]
  71.     }
  72.   }
  73. }
  74.  
  75. ######################################################################
  76. # routines to allow scrolling during text selection
  77. # from raines@cgibm1.slac.stanford.edu (Paul E. Raines)
  78. # modifications by js@bu.edu (Jay Sekora) to make it a little
  79. #   harder to make a selection, so you don't do it accidentally
  80. #   when you just want to move.
  81. ######################################################################
  82.  
  83. # j:tmb:move_sel W x y - move and begin a selection
  84. proc j:tmb:move_sel { W x y t } {
  85.   global j_teb J_PREFS tk_priv
  86.   set j_teb(dragscroll,txnd) 0
  87.   set j_teb(dragscroll,x) $x
  88.   set j_teb(dragscroll,y) $y
  89.   
  90.   set my_name [lindex [info level 0] 0]
  91.   set j_teb(last_command,$W) $my_name    ;# solves Emacs ^K, click, ^K problem
  92.   
  93.   if $J_PREFS(typeover) {
  94.     # clear current selection:
  95.     $W tag remove sel 1.0 end
  96.   }
  97.   # do what normal Tk binding does:
  98.   set tk_priv(selectMode) char
  99.   j:text:move $W @$x,$y
  100.   $W mark set anchor insert
  101.   if {[lindex [$W config -state] 4] == "normal"} {focus $W}
  102. }
  103.  
  104. # j:tmb:move W x y - move, don't change selection
  105. proc j:tmb:move { W x y t } {
  106.   global j_teb
  107.   set my_name [lindex [info level 0] 0]
  108.   set j_teb(last_command,$W) $my_name    ;# solves Emacs ^K, click, ^K problem
  109.   
  110.   j:text:move $W @$x,$y
  111.   $W mark set anchor insert
  112.   if {[lindex [$W config -state] 4] == "normal"} {focus $W}
  113. }
  114.  
  115. # j:tmb:drag_sel W x y - begin dragging out selection
  116. proc j:tmb:drag_sel { W x y t } {
  117.   global j_teb
  118.   
  119.   set xdiff [expr {abs($x - $j_teb(dragscroll,x))}]
  120.   set ydiff [expr {abs($y - $j_teb(dragscroll,y))}]
  121.   
  122.   if {$xdiff < 3 && $ydiff < 3} {
  123.     return
  124.   }
  125.   
  126.   if {$y > [winfo height $W]} {
  127.     if {!$j_teb(dragscroll,txnd)} {
  128.       after $j_teb(dragscroll,delay) j:tmb:extend_sel $W $x $y $t
  129.     }
  130.     set j_teb(dragscroll,txnd) 1
  131.     set j_teb(dragscroll,direction) down
  132.   } else {
  133.     if {$y < 0} {
  134.       if {!$j_teb(dragscroll,txnd)} {
  135.         after $j_teb(dragscroll,delay) j:tmb:extend_sel $W $x $y $t
  136.       }
  137.       set j_teb(dragscroll,txnd) 1
  138.       set j_teb(dragscroll,direction) up
  139.     } else {
  140.       set j_teb(dragscroll,txnd) 0
  141.       set j_teb(dragscroll,direction) 0
  142.     }
  143.   }
  144.  
  145.    if {!$j_teb(dragscroll,txnd)} {
  146.       tk_textSelectTo $W @$x,$y
  147.   }
  148. }
  149.  
  150. # j:tmb:extend_sel W x y t - drag out a selection, scrolling if necessary
  151. proc j:tmb:extend_sel { W x y t } {
  152.   global j_teb
  153.  
  154.   if {$j_teb(dragscroll,txnd)} {
  155.     if {$j_teb(dragscroll,direction) == "down"} {
  156.       tk_textSelectTo $W sel.last+1l
  157.       $W yview -pickplace sel.last+1l
  158.     } else {
  159.       if {$j_teb(dragscroll,direction) == "up"} {
  160.         tk_textSelectTo $W sel.first-1l
  161.         $W yview -pickplace sel.first-1l
  162.       } else { return }
  163.     }
  164.     after $j_teb(dragscroll,delay) j:tmb:extend_sel $W $x $y $t
  165.   }
  166. }
  167.  
  168. # j:tmb:end_sel W - finish a selection
  169. proc j:tmb:end_sel { W args } {
  170.   global j_teb
  171.   set j_teb(dragscroll,txnd) 0
  172. }
  173.  
  174. ######################################################################
  175. # set up text mouse bindings
  176. #   these supplement, rather than replace, the standard Tk bindings.
  177. ######################################################################
  178.  
  179. proc j:tb:mouse_bind { W } {
  180.   global j_teb
  181.   
  182.   j:tk3 {                ;# bindings are superfluous in Tk 4
  183.     # mouse bindings (for motion and scrolling selections)
  184.     bind $W <Button-1>        {j:tmb:move_sel %W %x %y %t}
  185.     bind $W <Control-Button-1>    {j:tmb:move %W %x %y %t}
  186.     bind $W <B1-Motion>        {j:tmb:drag_sel %W %x %y %t}
  187.     bind $W <ButtonRelease-1>    {j:tmb:end_sel %W %x %y %t}
  188.     
  189.     # mouse bindings (for scanning and pasting)
  190.     bind $W <Button-2>        {j:tmb:start_scan_or_paste %W %x %y %t}
  191.     bind $W <B2-Motion>        {j:tmb:continue_scan %W %x %y %t}
  192.     bind $W <ButtonRelease-2>    {j:tmb:end_scan_or_paste %W %x %y %t}
  193.   
  194.     # mouse bindings (for scanning and pasting)
  195.     bind $W <Button-3>        {j:tmb:start_scan_or_paste %W %x %y %t}
  196.     bind $W <B3-Motion>        {j:tmb:continue_scan %W %x %y %t}
  197.     bind $W <ButtonRelease-3>    {j:tmb:end_scan_or_paste %W %x %y %t}
  198.   }
  199. }
  200.  
  201.  
  202.